package com.ls.herostory.cmdhandler;

import com.ls.herostory.session.MoveState;
import com.ls.herostory.session.Session;
import com.ls.herostory.session.SessionUtil;
import com.ls.herostory.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Collection;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/10/12 9:33
 * @since
 */
public class WhoElseIsHereCmdHandler implements CmdHandler<GameMsgProtocol.WhoElseIsHereCmd> {

    @Override
    public void handleCmd(ChannelHandlerContext ctx, GameMsgProtocol.WhoElseIsHereCmd cmd) {
        GameMsgProtocol.WhoElseIsHereResult.Builder builder = GameMsgProtocol.WhoElseIsHereResult.newBuilder();

        Collection<Session> sessions = SessionUtil.getAllSession();
        for (Session session : sessions) {
            // 移动状态
            MoveState ms = session.getMoveState();
            GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState moveState =
                    GameMsgProtocol.WhoElseIsHereResult.UserInfo.MoveState.newBuilder()
                            .setFromPosX(ms.getFromPosX())
                            .setFromPosY(ms.getFromPosY())
                            .setToPosX(ms.getToPosX())
                            .setToPosY(ms.getToPosY())
                            .setStartTime(ms.getStartTime())
                            .build();

            // 用户信息
            GameMsgProtocol.WhoElseIsHereResult.UserInfo userInfo = GameMsgProtocol.WhoElseIsHereResult.UserInfo.newBuilder()
                    .setUserId(session.getUserId())
                    .setHeroAvatar(session.getHeroAvatar())
                    .setMoveState(moveState)
                    .build();

            builder.addUserInfo(userInfo);
        }

        GameMsgProtocol.WhoElseIsHereResult result = builder.build();
        ctx.writeAndFlush(result);
    }

}
